Weather = function(cityid, options){
	var imgpath = "js/weather/";
	var province = '上海'; //省
	var city = '上海'; //市
	var county = '上海'; //县、区
	var DEFAULT_CITY_ID = "101020100"; //上海
	var _cityId = DEFAULT_CITY_ID;

	var queryCityName = function(){
		if(typeof(remote_ip_info) != 'undefined'){
			var regex = /[^0-9a-zA-Z\\]+/;
			if(regex.test(remote_ip_info.province)){
				province = remote_ip_info.province;
			}
			if(regex.test(remote_ip_info.city)){
				city = remote_ip_info.city;
			}
			if(regex.test(remote_ip_info.district)){
				county = remote_ip_info.district;
			}
		}
	};

	var loadCityId = function(code){
		code = code || "";
		$.ajax({
			type: "GET",
			async: false,
			url: path+"/crossDomain.action?url=http://service.weather.com.cn/plugin/data/city"+code+".xml",
			dataType: "text",
			success: function(msg){
				if(msg){
					var cityArr = msg.split(",");
					if(cityArr){
						if(cityArr.length == 1){
							var citystr = cityArr[0].split("|");
							var citycode = citystr[0];
							var cityname = citystr[1];

							var cityId = cityname;
							if(cityId && cityId != province && cityId != city){
								_cityId = cityId;
							}else{
								if(citycode){
									loadCityId(citycode);
								}
							}
						}else{
							for(var i=0,j=cityArr.length; i<j; i++){
								var citystr = cityArr[i].split("|");
								var citycode = citystr[0];
								var cityname = citystr[1];
								
								if(cityname == province || cityname == city || cityname == county){
									loadCityId(citycode);
									break;
								}
							}
						}
					}
				}
			}
		});
	};
	
	var loadWeather = function(el, cityId){
		$.ajax({
			type: "GET",
			url: path+"/crossDomain.action?url=http://m.weather.com.cn/data/"+cityId+".html",
			dataType: "html",
			success: function(json){
				showWeather($.parseJSON(json));
				$.ajax({
					type: "post",
					url: path+"/cacheWeather.action",
					dataType: "json",
					data:{"province": province, "city": city, "county": county, "json": json}
				});
			}
		});
	};

	var showWeather = function(json){
		var str = [];
		for(var p in json){
			str.push(p+":"+json[p]);
		}
		if(json){
			var wi = json.weatherinfo;
			$("#title-city").html(wi.city);
			$("#title-date").html(wi.date_y);
			$("#title-week").html(wi.week);
			
			var date = new Date();
			if(date.getHours() < 18 && date.getHours() > 5){
				$("#title-w1-pic").html("<img src='"+imgpath+"pic/d"+wi.img1+".gif'/>");
				$("#title-w1-txt").html(wi.img_title1);
			}else{
				$("#title-w1-pic").html("<img src='"+imgpath+"pic/n"+transImgTitle(wi.img1, wi.img2)+".gif'/>");
				$("#title-w1-txt").html(wi.img_title2);
			}
			$("#title-w1-temp").html(wi.temp1);
			$("#title-w1-wind").html(wi.wind1);

			$("#w1-pic-d").html("<img src='"+imgpath+"pic/d"+wi.img1+".gif'/>");
			$("#w1-txt-d").html(wi.img_title1);
			$("#w1-temp").html(wi.temp1);
			$("#w1-wind").html(wi.wind1);
			$("#w1-pic-n").html("<img src='"+imgpath+"pic/n"+transImgTitle(wi.img1, wi.img2)+".gif'/>");
			$("#w1-txt-n").html(wi.img_title2);
			
			$("#w2-pic-d").html("<img src='"+imgpath+"pic/d"+wi.img3+".gif'/>");
			$("#w2-txt-d").html(wi.img_title3);
			$("#w2-temp").html(wi.temp2);
			$("#w2-wind").html(wi.wind2);
			$("#w2-pic-n").html("<img src='"+imgpath+"pic/n"+transImgTitle(wi.img3, wi.img4)+".gif'/>");
			$("#w2-txt-n").html(wi.img_title4);

			$("#w3-pic-d").html("<img src='"+imgpath+"pic/d"+wi.img5+".gif'/>");
			$("#w3-txt-d").html(wi.img_title5);
			$("#w3-temp").html(wi.temp3);
			$("#w3-wind").html(wi.wind3);
			$("#w3-pic-n").html("<img src='"+imgpath+"pic/n"+transImgTitle(wi.img5, wi.img6)+".gif'/>");
			$("#w3-txt-n").html(wi.img_title6);
			
			$("#weather_container").tipTip({
				activation : 'click',
				keepAlive: true,
				maxWidth: '500px',
				content: $("#weatherContainer").html()
			});
		}else{
			//alert("访问天气网错误！");
		}
	};

	var transImgTitle = function(imgTitle1, imgTitle2){
		var it = parseInt(imgTitle2);
		if(it ==  99){
			return imgTitle1;
		}
		return imgTitle2;
	};

	this.render = function(el){
		var el = el || $(document.body);
		$.ajax({
			type: "POST",
			url: path+"/getWeather.action",
			dataType: "json",
			data:{"province": province, "city": city, "county": county},
			success: function(json){
				if(json.data){
					var date = json.data.date;
					var cacheDate = new Date(date);
					var curDate = new Date();
					if(cacheDate.getYear() == curDate.getYear() && 
						cacheDate.getMonth() == curDate.getMonth() &&
						cacheDate.getDate() == curDate.getDate() && 
						( curDate.getHours() - cacheDate.getHours() ) <= 4 ){
						showWeather(json.data);
						return;
					}
				}
				loadCityId();
				loadWeather(el, _cityId);
			}
		});
	};
	
	(function(){
		queryCityName();
	})();
};